//
//  ScroButton.m
//  scroButton
//
//  Created by 刘建峰 on 16/7/6.
//  Copyright © 2016年 chenling. All rights reserved.
//

#import "ScroButton.h"
#import "AdaptScreenHelp.h"

#define gap 1   //边缘缝隙

@interface ScroButton ()
@property(nonatomic,strong)UILabel * backLabel;
@property(nonatomic,strong)UIView * circleView;
@property(nonatomic,strong)UIButton * button;
@property(nonatomic,assign)CGFloat width;
@property(nonatomic,assign)CGFloat height;
@property(nonatomic,assign)CGFloat bigCorner;
@property(nonatomic,assign)CGFloat smallCorner;
@end


@implementation ScroButton

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        _width = frame.size.width/VerticalRatio();
        _height = frame.size.height/VerticalRatio();
        _bigCorner = (_height/2)*VerticalRatio();
        _smallCorner = ((_height-2*gap)/2)*VerticalRatio();
        
        self.layer.cornerRadius = _bigCorner;
        
        _backLabel = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(_height/4, 0, _width-_height/2, _height), NO)];
        _backLabel.layer.masksToBounds = YES;
        _backLabel.backgroundColor = [UIColor clearColor];
        _backLabel.textAlignment = NSTextAlignmentLeft;
        _backLabel.layer.cornerRadius = _bigCorner;
        [self addSubview:_backLabel];
        
        
        _circleView = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(gap, gap, _height-2*gap, _height-2*gap), NO)];
        _circleView.backgroundColor = [UIColor whiteColor];
        _circleView.layer.cornerRadius = _smallCorner;
        [self addSubview:_circleView];
        
        
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = flexibleFrame(CGRectMake(0, 0, _width, _height), NO);
        _button.backgroundColor = [UIColor clearColor];
        [_button addTarget:self action:@selector(changeSelected:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_button];
        
    }
    return self;
}


- (void)changeSelected:(UIButton *)sender{
    sender.selected = !sender.selected;
    _ifSelected = sender.selected;
    _changeSelected(_ifSelected);
    if (sender.selected) {
         _circleView.frame = flexibleFrame(CGRectMake(_width - (_height- 2*gap) - gap, gap, _height-2*gap, _height-2*gap), NO);
        _backLabel.textAlignment = NSTextAlignmentLeft;
        self.backgroundColor = _selectedColor;
        _backLabel.text = _selectedTitle;
    }else{
        _circleView.frame = flexibleFrame(CGRectMake(gap, gap, _height-2*gap, _height-2*gap), NO);
        _backLabel.textAlignment = NSTextAlignmentRight;
        self.backgroundColor = _defaultColor;
        _backLabel.text = _defaultTitle;
    }

}


- (void)setDefaultColor:(UIColor*)defaultColor{
    if (_defaultColor != defaultColor) {
        _defaultColor = defaultColor;
        self.backgroundColor = _defaultColor;
    }

}

- (UIColor *)defaultColor{
    return _defaultColor;
}

- (void)setSelectedColor:(UIColor*)selectedColor{
    if (_selectedColor != selectedColor) {
        _selectedColor = selectedColor;
    }
    
}

- (UIColor *)selectedColor{
    return _defaultColor;
}





- (void)setIfSelected:(BOOL)ifSelected{
    if (_ifSelected !=ifSelected) {
        _ifSelected = ifSelected;
        _button.selected = _ifSelected;
        if (_ifSelected) {
            _circleView.frame = flexibleFrame(CGRectMake(_width - (_height- 2*gap) - gap, gap, _height-2*gap, _height-2*gap), NO);
            _backLabel.textAlignment = NSTextAlignmentLeft;
            self.backgroundColor = _selectedColor;
            _backLabel.text = _selectedTitle;
        }else{
            _circleView.frame = flexibleFrame(CGRectMake(gap, gap, _height-2*gap, _height-2*gap), NO);
            _backLabel.textAlignment = NSTextAlignmentRight;
            self.backgroundColor = _defaultColor;
            _backLabel.text = _defaultTitle;
        }
    }
}

- (BOOL)ifSelected{
    return _ifSelected;
}


- (void)setCircleColor:(UIColor *)circleColor{
    if (_circleColor != circleColor) {
        _circleColor = circleColor;
        _circleView.backgroundColor = _circleColor;
    }
}

- (UIColor *)circleColor{
    return _circleColor;
}

- (void)setDefaultTitle:(NSString *)defaultTitle{
    if (_defaultTitle != defaultTitle) {
        _defaultTitle = defaultTitle;
        _backLabel.text = _defaultTitle;
    }
}

- (NSString *)defaultTitle{
    return _defaultTitle;
}

- (void)setSelectedTitle:(NSString *)selectedTitle{
    if (_selectedTitle != selectedTitle) {
        _selectedTitle = selectedTitle;
    }
}

- (NSString *)selectedTitle{
    return _defaultTitle;
}

- (void)setTextColor:(UIColor *)textColor{
    if (_textColor != textColor) {
        _textColor = textColor;
        _backLabel.textColor = _textColor;
    }
}

- (UIColor *)textColor{
    return _textColor;
}


- (void)setTextFont:(UIFont *)textFont{
    if (_textFont != textFont) {
        _textFont = textFont;
        _backLabel.font = textFont;
    }
}

- (UIFont *)textFont{
    return _textFont;
}

@end
